package l.l.w.leetcode.ReverseInteger;

/**
 * Created by llw on 2017/8/29.
 */
public class ReverseInteger {
    public int reverse(int x) {
            if (x == Integer.MIN_VALUE)
                return 0;
            int[] arr = new int[10];
            boolean isPositive;
            if(x>0) isPositive=true;
            else {
                isPositive=false;
                x=-x;
            }
            int k =0;
            while(x!=0){
                arr[k++]=x%10;
                x=x/10;
            }
            int result=0;
            int toAdd;
            for(int i=0;i<k;++i){
                toAdd=arr[i];
                if((Integer.MAX_VALUE-toAdd)/10<result)
                    return 0;
                result *=10;
                result+=toAdd;
            }
            if(!isPositive){
                return -1* result;
            }
            return result;
    }

    public static void main(String[] args) {
        int a =2147483646;
        System.out.println(new ReverseInteger().reverse(a));
    }
}
